package com.xtkj.superclub_information.controller;
import com.xtkj.commons.JsonResult;
import com.xtkj.commons.SnowflakeIdWorker;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lizheqi
 * @create 2020-01-04-11:28
 */
@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 获取资讯列表
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getNews",method = RequestMethod.GET)
    public JsonResult getNews() throws Exception{
        //创建HttpClient连接池
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        //最大连接数
        cm.setMaxTotal(100);
        //每台主机最大的连接数
        cm.setDefaultMaxPerRoute(10);
        //获取HttpClient对象
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
        //获取HttpGet对象
        URIBuilder uriBuilder = new URIBuilder("http://news.baidu.com/");
        HttpGet httpGet = new HttpGet(uriBuilder.build());
        //添加配置
        RequestConfig config = RequestConfig.custom().setConnectTimeout(10000).setConnectionRequestTimeout(30000).setSocketTimeout(30000).build();
        httpGet.setConfig(config);
        Map<String,Object> outMap = new HashMap<>();
        //发送请求,获取响应
        CloseableHttpResponse response = httpClient.execute(httpGet);
        if(response.getStatusLine().getStatusCode()==200){
            String html = EntityUtils.toString(response.getEntity());
            //获取得到字符串,解析响应并得到dom对象
            Document doc = Jsoup.parse(html);
            //获取所有的热点信息的所有的li元素
            Elements elements = doc.select("div.hotnews > ul > li");
            System.out.println(elements.size());
            for (Element element:elements){
                //获取资讯主题
                String title = element.text();
                System.out.println(element.text());
                //获取资讯列表链接地址
                String href = element.getElementsByAttribute("href").attr("href");
                //将资讯主题以及链接地址存放到map集合
                //再次根据该咨询的链接地址获取里面的第一张图片作为前端显示
                Document document = Jsoup.parse(new URL(href), 10000);
                //获取首页的第一页图片
                String src = document.select("img").first().attr("src");
                HashMap<String, String> innerMap = new HashMap<>();
                innerMap.put("title",title);
                innerMap.put("href",href);
                innerMap.put("src",src);
                outMap.put(snowflakeIdWorker.nextId()+"",innerMap);
            }
            return JsonResult.success().setData(outMap).setCode(200).setMsg("OK");
        }
        return JsonResult.error().setCode(400).setData(outMap).setMsg("状态异常");
    }

}
